package newcoder;


import java.util.Stack;
import java.util.regex.Pattern;

/**
 * @Author 肖地洋
 * @Date 2022/6/21 19:31
 */
public class NC199 {
    public String decodeString (String s) {
        Stack<String> stack = new Stack();
        for(int i=0;i<s.length();i++){
            int start =i;
            if(Character.isLetter(s.charAt(i))){ //是字母
                while(i<s.length()&&Character.isLetter(s.charAt(i))){
                    i++;
                }
                stack.add(s.substring(start,i--));
            }else if(Character.isDigit(s.charAt(i))){ //是数字
                while (i<s.length()&&Character.isDigit(s.charAt(i))){
                    i++;
                }
                stack.add(s.substring(start,i--));
            }else if(s.charAt(i)==']'){
                String sub = stack.pop();
                while(!Pattern.compile("[0-9]*").matcher(stack.peek()).matches()){
                    sub = stack.pop()+sub;
                }
                Integer n = Integer.valueOf(stack.pop());
                String str = "";
                for(int j = 0;j<n;j++){
                    str+=sub;
                }
                stack.add(str);
            }
        }
        String target =stack.pop();
        while (!stack.isEmpty()) target = stack.pop()+target;
        return  target;
    }

    public static void main(String[] args) {
        System.out.println(new NC199().decodeString("3[abc]2[abc]"));
    }
}
